package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yljf.domain.Record;
import com.ruoyi.yljf.domain.UserScoreRecord;
import com.ruoyi.yljf.domain.vo.*;
import com.ruoyi.yljf.mapper.RecordMapper;
import com.ruoyi.yljf.mapper.UserScoreRecordMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.CoursePackageMapper;
import com.ruoyi.yljf.domain.CoursePackage;
import com.ruoyi.yljf.service.ICoursePackageService;

import javax.annotation.Resource;

/**
 * 课程目录Service业务层处理
 * 
 * @author metaee
 * @date 2025-03-06
 */
@Service
public class CoursePackageServiceImpl extends ServiceImpl<CoursePackageMapper, CoursePackage> implements ICoursePackageService {

    @Resource
    private CoursePackageMapper coursePackageMapper;

    @Resource
    private UserScoreRecordMapper userScoreRecordMapper;

    @Resource
    private RecordMapper recordMapper;
    
    /**
     * 查询课程目录列表
     * 
     * @param coursePackage 课程目录
     * @return 课程目录
     */
    @Override
    public List<CoursePackage> selectCoursePackageList(CoursePackage coursePackage) {
    	LambdaQueryWrapper<CoursePackage> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(coursePackage.getName())) {
			queryWrapper.like(CoursePackage::getName, coursePackage.getName());
		}
        if (StringUtils.isNotEmpty(coursePackage.getTitle())) {
			queryWrapper.eq(CoursePackage::getTitle, coursePackage.getTitle());
		}
        if (StringUtils.isNotNull(coursePackage.getCourseId())) {
			queryWrapper.eq(CoursePackage::getCourseId, coursePackage.getCourseId());
		}
        if (StringUtils.isNotEmpty(coursePackage.getFilePath())) {
			queryWrapper.eq(CoursePackage::getFilePath, coursePackage.getFilePath());
		}
        if (StringUtils.isNotEmpty(coursePackage.getFileDuration())) {
			queryWrapper.eq(CoursePackage::getFileDuration, coursePackage.getFileDuration());
		}
        if (StringUtils.isNotEmpty(coursePackage.getType())) {
			queryWrapper.eq(CoursePackage::getType, coursePackage.getType());
		}
        return coursePackageMapper.selectList(queryWrapper);
    }

    /**
     * 根据课程 id 获取章节列表
     * @param courseId
     * @return
     */
    @Override
    public List<PackageVO> getByCourseID(Long courseId) {
        // 创建一个结果列表，存储自定义对象
        List<PackageVO> resultList = new ArrayList<>();
        List<String> s = coursePackageMapper.distinctBySections(courseId);

        List<CoursePackage> coursePackages = coursePackageMapper.selectList(new LambdaQueryWrapper<>(CoursePackage.class)
                .eq(ObjectUtil.isNotEmpty(courseId),CoursePackage::getCourseId, courseId)
        );
        for (String section : s) {
            // 过滤出 sections 包含当前 section 的 CoursePackage 对象
            List<CoursePackageVO> filteredList = coursePackages.stream()
                    .filter(coursePackage -> coursePackage.getSections().contains(section))
                    .map(coursePackage -> {
                        CoursePackageVO vo = BeanUtil.copyProperties(coursePackage,CoursePackageVO.class);
                        Record record = recordMapper.selectOne(new LambdaQueryWrapper<>(Record.class)
                                .eq(Record::getUserId, SecurityUtils.getUserId())
                                .eq(Record::getCourseId, courseId)
                                .eq(Record::getSectionId, vo.getId()));
                        if(record != null) {
                            vo.setPlaybackTime(record.getOffTime().toString());
                        }
                        return vo;
                    })
                    .collect(Collectors.toList());
            // 创建自定义对象并添加到结果列表
            PackageVO customResult = new PackageVO();
            customResult.setSections(section);
            customResult.setList(filteredList);
            resultList.add(customResult);
        }
        return resultList;
    }

    @Override
    public CoursePackageResVO selectById(Long id) {
        CoursePackage coursePackage = this.baseMapper.selectById(id);
        CoursePackageResVO vo = new CoursePackageResVO(coursePackage);
        vo.setNoticeType("course");
        return vo;
    }

    @Override
    public List<CoursePackagePracticeVO> getCoursePackagePracticeByCourseID(Long courseId) {
        return coursePackageMapper.selectByCourseID(courseId);
    }

    @Override
    public List<PackageScoreRecordVO> getCourseScoreRecord(Long id) {
        List<UserScoreRecord> userScoreRecords = userScoreRecordMapper.selectList(new LambdaQueryWrapper<>(UserScoreRecord.class)
                .eq(UserScoreRecord::getUserId, SecurityUtils.getUserId())
                .eq(UserScoreRecord::getOtherId, id)

        );

        return BeanUtil.copyToList(userScoreRecords, PackageScoreRecordVO.class);
    }
}
